home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / lib / ttymode.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  2KB  |  57 lines

  1. /*
  2.  * Copy the existing mode of a terminal to another terminal.
  3.  * Typically this is used to initialize a slave pseudo-terminal
  4.  * to the state of the terminal associated with standard input.
  5.  *
  6.  * We provide two functions to do this in 2 separate steps.
  7.  */
  8.  
  9. #include    <sys/types.h>
  10. #include    <sys/ioctl.h>
  11.  
  12.     /* See the tty(4) man page for all the details */
  13. static struct sgttyb    tty_sgttyb;    /* basic modes (V6 & V7) */
  14. static struct tchars    tty_tchars;    /* basic control chars (V7) */
  15. static struct ltchars    tty_ltchars;    /* control chars for new discipline */
  16. static struct winsize    tty_winsize;    /* terminal and window sizes */
  17. static int        tty_localmode;    /* local mode word */
  18. static int        tty_ldisc;    /* line discipline word */
  19.  
  20. /*
  21.  * Get a copy of the tty modes for a given file descriptor.
  22.  * The copy is then used later by tty_setmode() below.
  23.  */
  24.  
  25. int
  26. tty_getmode(oldfd)
  27. int    oldfd;        /* typically an actual terminal device */
  28. {
  29.     if (ioctl(oldfd, TIOCGETP,   (char *) &tty_sgttyb) < 0)     return(-1);
  30.     if (ioctl(oldfd, TIOCGETC,   (char *) &tty_tchars) < 0)     return(-1);
  31.     if (ioctl(oldfd, TIOCGLTC,   (char *) &tty_ltchars) < 0)    return(-1);
  32.     if (ioctl(oldfd, TIOCLGET,   (char *) &tty_localmode) < 0)  return(-1);
  33.     if (ioctl(oldfd, TIOCGETD,   (char *) &tty_ldisc) < 0)      return(-1);
  34.     if (ioctl(oldfd, TIOCGWINSZ, (char *) &tty_winsize) < 0)    return(-1);
  35.  
  36.     return(0);
  37. }
  38.  
  39. /*
  40.  * Set the tty modes for a given file descriptor.
  41.  * We set the modes from the values saved by tty_getmode() above.
  42.  */
  43.  
  44. int
  45. tty_setmode(newfd)
  46. int    newfd;        /* typically a pseudo-terminal slave device */
  47. {
  48.     if (ioctl(newfd, TIOCSETP,   (char *) &tty_sgttyb) < 0)     return(-1);
  49.     if (ioctl(newfd, TIOCSETC,   (char *) &tty_tchars) < 0)     return(-1);
  50.     if (ioctl(newfd, TIOCSLTC,   (char *) &tty_ltchars) < 0)    return(-1);
  51.     if (ioctl(newfd, TIOCLSET,   (char *) &tty_localmode) < 0)  return(-1);
  52.     if (ioctl(newfd, TIOCSETD,   (char *) &tty_ldisc) < 0)      return(-1);
  53.     if (ioctl(newfd, TIOCSWINSZ, (char *) &tty_winsize) < 0)    return(-1);
  54.  
  55.     return(0);
  56. }
  57.